package com.baidu.mapsdkexample.mapshow.placechoose;

import java.util.List;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapsdkexample.R;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class PoiItemAdapter extends RecyclerView.Adapter {
    private List<PoiInfo> mPoiInfos; // poi信息

    private OnItemClickListener mOnItemClickListener;

    private int mChosePosition = 0;

    public PoiItemAdapter(String address, List<PoiInfo> poiINfoList) {
        mPoiInfos = poiINfoList;
    }

    public void updateData(String address, List<PoiInfo> poiInfos) {
        mChosePosition = 0;
        mPoiInfos = poiInfos;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        public View mPoiItemView;
        public TextView mPoiNameText;
        public TextView mPoiAddressText;
        public View mBtnSetEnd;

        public MyViewHolder(View view) {
            super(view);
            mPoiItemView = view;
            mPoiNameText = view.findViewById(R.id.poiResultName);
            mPoiAddressText = view.findViewById(R.id.poiResultDetail);
            mBtnSetEnd = view.findViewById(R.id.btnSetEnd);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.baidumap_rgc_item, parent, false);

        final MyViewHolder myViewHolder = new MyViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mChosePosition = (int)view.getTag();
                    notifyDataSetChanged();
                    mOnItemClickListener.onItemClick(view, (int)view.getTag());
                }
            }
        });
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (position < 0) {
            return;
        }

       final MyViewHolder myViewHolder = (MyViewHolder) holder;

        myViewHolder.mPoiItemView.setTag(position);

        if (null != mPoiInfos && mPoiInfos.size() > 0) {
            bindPoiInfo(myViewHolder, position);
        }
    }

    private void bindPoiInfo(MyViewHolder viewHolder, int position) {
        if (null == mPoiInfos || mPoiInfos.size() <= 0) {
            return;
        }

        PoiInfo poiInfo = mPoiInfos.get(position);
        if (null == poiInfo) {
            return;
        }

        String name = "";
        if (0 == position) {
            name = "【" + poiInfo.getName() + "】";
        } else {
            name = poiInfo.getName();
        }

        viewHolder.mPoiNameText.setText(name);

        if (!TextUtils.isEmpty(poiInfo.getAddress())) {
            viewHolder.mPoiAddressText.setVisibility(View.VISIBLE);
            viewHolder.mPoiAddressText.setText(poiInfo.getAddress());
        } else {
            viewHolder.mPoiAddressText.setVisibility(View.GONE);
        }

        if (mChosePosition == position) {
            viewHolder.mBtnSetEnd.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mBtnSetEnd.setVisibility(View.GONE);
        }
    }

    /**
     * Returns the total number of items in the data set held by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        if (null != mPoiInfos) {
            return mPoiInfos.size();
        }

        return 0;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
